﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using RBAC.ApiReads.Application.Command;
using RBAC.ApiReads.DTO;
using RBAC.Domain.RbacEntity;
using RBAC.ResultCode;
using StackExchange.Redis;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;

namespace RBAC.ApiReads.Controllers
{
    /// <summary>
    /// API控制器 读
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class ManagementController : ControllerBase
    {
        private readonly IMediator mediator;
        private readonly ILogger<ManagementController> logger;


        public ManagementController(IMediator mediator,ILogger<ManagementController> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="request">请求</param>
        /// <returns>返回登录任务</returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<ApiResult<UserDTO>> Login(GetLoginCommand request)
        {
            try
            {
                logger.LogInformation("登录日志信息");
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);

                throw;
            }
        }

        




        /// <summary>
        /// 权限列表
        /// </summary>
        /// <returns>返回权限列表信息</returns>
        [HttpGet]
        public async Task<ApiResult<List<Permission>>> GetPermissionlist([FromQuery] GetPermissionCommand request)
        {
            try
            {
                logger.LogInformation("权限列表日志信息");
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);

                throw;
            }
        }
        /// <summary>
        /// 角色权限列表
        /// </summary>
        /// <returns>返回集合</returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<ApiResult<ApiPaging<List<RolePermissionDTO>>>> GetRolePermission([FromQuery] GetRolePermissionCommand request)
        {
            try
            {
                logger.LogInformation("日志信息");
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);

                throw;
            }
        }
        /// <summary>
        /// 用户角色列表
        /// </summary>
        /// <returns>返回集合</returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<ApiResult<ApiPaging<List<UserRoleDTO>>>> GetUserRole([FromQuery]  GetUserRoleCommand request)
        {
            try
            {
                logger.LogInformation("日志信息");
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);

                throw;
            }
        }
        /// <summary>
        /// 功能列表
        /// </summary>
        /// <param name="request">请求</param>
        /// <returns>返回列表信息</returns>
        [HttpGet]
        public async Task<ApiResult<List<Function>>> GetFunction()
        {
            try
            {
                logger.LogInformation("功能列表日志信息");
                return await mediator.Send(new GetFunctionCommand());
            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);

                throw;
            }
        }
        }
}
